package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 北京银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class BeiJingBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Origin", "https://www.bankofbeijing.com.cn"); // 仿真
        headers.put("Referer", "https://www.bankofbeijing.com.cn/personal/dkll"); // 仿真
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.bankofbeijing.com.cn/api/webListModel/queryFinInfo");

        Map<String, String> params = new HashMap<>();
        params.put("pageId", "1564892216314220544");
        params.put("channel", "1");
        params.put("language", "CN");

        String requestParams = "{\"pageId\":\"1564892216314220544\",\"channel\":1,\"language\":\"CN\"}";
        Connection.Response execute = connect.requestBody(requestParams).headers(headers).ignoreContentType(true).method(Connection.Method.POST).execute();


        JSONObject responseJson = new JSONObject(execute.body());
        Document responseDoc = Jsoup.parse(responseJson.getJSONObject("data").getStr("content"));
        Element table = responseDoc.select("table").get(1);

        String kaiShiZhiXingRiQi = table.selectXpath("tbody/tr[1]/td[1]/span").text();
        String huoQiCunKuanLiLv = table.selectXpath("tbody/tr[3]/td[2]/span").text();
        String zhengCunZhengQu3YueLiLv = table.selectXpath("tbody/tr[6]/td[2]/span").text();
        String zhengCunZhengQu6YueLiLv = table.selectXpath("tbody/tr[7]/td[2]/span").text();
        String zhengCunZhengQu12YueLiLv = table.selectXpath("tbody/tr[8]/td[2]/span").text();
        String zhengCunZhengQu24YueLiLv = table.selectXpath("tbody/tr[9]/td[2]/span").text();
        String zhengCunZhengQu36YueLiLv = table.selectXpath("tbody/tr[10]/td[2]/span").text();
        String zhengCunZhengQu60YueLiLv = table.selectXpath("tbody/tr[11]/td[2]/span").text();

        String lingCunZhengQu12YueLiLv = table.selectXpath("tbody/tr[13]/td[2]/span").text();
        String lingCunZhengQu36YueLiLv = table.selectXpath("tbody/tr[14]/td[2]/span").text();
        String lingCunZhengQu60YueLiLv = table.selectXpath("tbody/tr[15]/td[2]/span").text();

        String dingHuoLiangBianMiaoShu = table.selectXpath("tbody/tr[16]/td[2]/span").text();

        String dingHuoLiangBian3YueLiLv = null;
        String dingHuoLiangBian6YueLiLv = null;
        String dingHuoLiangBian12YueLiLv = null;
        if (Objects.equals(dingHuoLiangBianMiaoShu, "按一年以内定期整存整取同档次利率打六折执行")) {
            dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
            dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
            dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        }

        String tongZhiCunKuan1TianLiLv = table.selectXpath("tbody/tr[18]/td[2]/span").text();
        String tongZhiCunKuan7TianLiLv = table.selectXpath("tbody/tr[19]/td[2]/span").text();

        String xieDingCunKuanLiLv = table.selectXpath("tbody/tr[20]/td[2]/span").text();

        return new BankRate(DateFormatUtil.format2(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, getXieDingCunKuanLiLv(xieDingCunKuanLiLv));
    }

    @Override
    public String getBankName() {
        return "北京银行";
    }


    private String getXieDingCunKuanLiLv(String xieDingCunKuanLiLv) {
        if (Objects.equals("-", xieDingCunKuanLiLv)) {
            return null;
        }
        return xieDingCunKuanLiLv;
    }
}
